Navigation

  • index
  • next |
  • previous |
  • PyHowTo documentation »
  • Basic »
  • Dictionary »

Table of Contents

Python v3.7 HowTos:

  • ----------------
  • Recursion
  • Backtracking
  • Dynamic Programming
  • Greedy
  • Sort
  • Binary Search
  • Depth First Search [DFS]
  • Breadth First Search [BFS]
  • Binary Search Tree [BST]
  • ----------------
  • Array
  • String
  • Heap
  • Stack
  • Queue
  • Tree
  • Linked List
  • Hash Table
  • Bit Manipulation
  • Two Pointers
  • Math
  • Decorator
  • ----------------
  • Basic
  • Intermediate
  • Advanced
  • Interview
  • ----------------
  • Spark
  • Tkinter
  • Turtle
  • Games
  • Web
  • ----------------
  • About
  • History

Previous topic

Generate a dictionary of numbers (1..N) in form (x, x*x)

Next topic

Create a dictionary from a string tracking the count of the letters

Quick search

Print a dictionary where the keys (1..15) and values=key*keyΒΆ

Write a python script to print a dictionary where the keys are numbers between
1 and 15 (both included) and the values are square of keys.
Expected output:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64,
9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}
D = dict()
for k in range(1, 16):
    D[k] = k**2
print(D)

Output:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81,
 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}

See also

https://www.w3resource.com/python-exercises/dictionary/python-data-type-dictionary-exercise-7.php

Navigation

  • index
  • next |
  • previous |
  • PyHowTo documentation »
  • Basic »
  • Dictionary »
© Copyright 2020, Sergiy Zaytsev, szaytsev@hotmail.com. Created using Sphinx 2.3.0.